﻿using DropBoxClient.Entities;
using DropBoxClient.Framework;
using DropBoxClient.Operations;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;

namespace DropBoxClient
{
    public class OAuth2Client
    {
        private string _accessToken;

        public OAuth2Client(string accessToken)
        {
            _accessToken = accessToken; 
        }

        public AccountInfo GetAccountInfo()
        {
            var operation = new GetAccountInfoOperation(_accessToken);
            return operation.Execute();
        }

        public FileContent GetFiles(string root, string path)
        {
            var operation = new GetFilesOperation(_accessToken);
            operation.Root = root;
            operation.Path = path;
            return operation.Execute();
        }

        public Meta PutFiles(string root, string path, string inputFilePath)
        {
            var operation = new PutFilesOperation(_accessToken);
            operation.Root = root;
            operation.Path = path;
            operation.InputPath = inputFilePath;
            return operation.Execute();
        }

        public Meta PutFiles(string root, string path, Stream inputFileStream)
        {
            var operation = new PutFilesOperation(_accessToken);
            operation.Root = root;
            operation.Path = path;
            operation.InputStream = inputFileStream;
            return operation.Execute();
        }

        public FileMeta GetFileMeta(string root, string path)
        {
            var operation = new GetFileMetaOperation(_accessToken);
            operation.Root = root;
            operation.Path = path;
            return operation.Execute();
        }

        public FolderMeta GetFolderMeta(string root, string path)
        {
            var operation = new GetFolderMetaOperation(_accessToken);
            operation.Root = root;
            operation.Path = path;
            return operation.Execute();
        }

        public DropBoxState GetDelta(string cursor)
        {
            var operation = new PostDeltaOperation(_accessToken);
            operation.Cursor = cursor;
            return operation.Execute();
        }

        public List<FileMeta> GetRevisions(string root, string path)
        {
            var operation = new GetRevisionsOperation(_accessToken);
            operation.Root = root;
            operation.Path = path;
            return operation.Execute();
        }

        public Meta Restore(string root, string path, string revision)
        {
            var operation = new PostRestoreOperation(_accessToken);
            operation.Root = root;
            operation.Path = path;
            operation.Revision = revision;
            return operation.Execute();
        }

        public List<Meta> Search(string root, string path, string query)
        {
            var operation = new SearchOperation(_accessToken);
            operation.Root = root;
            operation.Path = path;
            operation.Query = query;
            return operation.Execute();
        }

        public Share GetShares(string root, string path)
        {
            var operation = new PostSharesOperation(_accessToken);
            operation.Root = root;
            operation.Path = path;
            return operation.Execute();
        }

        public Share GetMedia(string root, string path)
        {
            var operation = new PostMediaOperation(_accessToken);
            operation.Root = root;
            operation.Path = path;
            return operation.Execute();
        }

        public CopyReference GetCopyRef(string root, string path)
        {
            var operation = new GetCopyRefOperation(_accessToken);
            operation.Root = root;
            operation.Path = path;
            return operation.Execute();
        }

        public ImageContent GetThumbnails(string root, string path)
        {
            var operation = new GetThumbnailsOperation(_accessToken);
            operation.Root = root;
            operation.Path = path;
            return operation.Execute();
        }

        public Meta Copy(string root, string fromPath, string toPath)
        {
            var operation = new CopyOperation(_accessToken);
            operation.Root = root;
            operation.FromPath = fromPath;
            operation.ToPath = toPath;
            return operation.Execute();
        }

        public Meta CreateFolder(string root, string path)
        {
            var operation = new CreateFolderOperation(_accessToken);
            operation.Root = root;
            operation.Path = path;
            return operation.Execute();
        }

        public Meta Delete(string root, string path)
        {
            var operation = new DeleteOperation(_accessToken);
            operation.Root = root;
            operation.Path = path;
            return operation.Execute();
        }

        public Meta Move(string root, string fromPath, string toPath)
        {
            var operation = new MoveOperation(_accessToken);
            operation.Root = root;
            operation.FromPath = fromPath;
            operation.ToPath = toPath;
            return operation.Execute();
        }
    }
}
